#include <iostream>
#include <chrono>
#include <iomanip>

// windows下精度大概5ms左右用于基本的测量调用函数的间隔
class TimeTracker
{
private:
    std::chrono::steady_clock::time_point last_time;
    bool is_first_call = true;

public:
    void track(const std::string &message = "")
    {
        auto current_time = std::chrono::steady_clock::now();

        if (is_first_call)
        {
            is_first_call = false;
            last_time = current_time;
            return;
        }

        // 使用纳秒级别的计时，提高精度
        auto duration_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(current_time - last_time);

        // 转换成浮点数毫秒显示，保留3位小数
        double ms = duration_ns.count() / 1000000.0;
        std::cout << (message.empty() ? "Time interval" : message)
                  << ": " << std::fixed << std::setprecision(3)
                  << ms << " milliseconds" << std::endl;

        last_time = current_time;
    }
};
// 增加一个不需要声明类直接用函数调用的快捷方法
void track(const std::string &message = "")
{
    static TimeTracker tt;
    tt.track(message);
}
